package controllers;

import models.TA001;
import models.TA014;
import org.dom4j.Document;
import play.mvc.Controller;
import utils.*;

import java.util.List;

/**
 * Created by xuyang on 2018/1/11.
 */
public class TemplateTester extends Controller {

    public static void index(String datagramText) {

        if (datagramText != null) {

            String fixedText = ACARS620Parser.clearDatagramControlChar(datagramText);


            TA001 acars = null;

            List<TA014> templates = null;
            List<TA014> guessedTemplates = null;

            try {
                acars = ACARS620Parser.parse(fixedText);

                templates = TemplateMatcher.matchedTemplatesForACARS(acars);

                guessedTemplates = TemplateMatcher.guessMatchedTemplateForACARS(acars);

            } catch (ACARS620Exception e) {
                e.printStackTrace();
                flash.error(e.getLocalizedMessage());
            }


            render(datagramText, acars, templates, guessedTemplates);
        }

        render(datagramText);
    }

    public static void doSplit(Long id, String datagramText) {
        TA014 ta014 = TA014.findById(id);

        String template = ta014.template;

        Document result = doSplitWithTemplate(template, datagramText);

        renderXml(result.asXML());
    }

    private static Document doSplitWithTemplate(String template, String datagramText) {

        TemplateSplittor templateSplittor = new TemplateSplittor(template, datagramText);

        return templateSplittor.invoke();
    }
}
